﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Messaging;
using Tamir.SharpSsh;

namespace SSHTool
{
    public partial class HandHandleMode : Form
    {
        public string Ip = string.Empty;
        public string User = string.Empty;
        public string PWD = string.Empty;


        private int DK = 0;
        private int CommandTimeoutSeconds = 300; /*连接超时5分钟*/
        public bool IsConnected = false;
        Form2 form2 = null;
        /// <summary>
        /// 具体做事情的委托 ，方便 处理线程 跟UI的线程分开处理  实现进度的管理
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        delegate void  Delegate_DoIt();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="isConnect"></param>
        delegate void Delagete_update(bool isConnect);

        /// <summary>
        /// SSH连接的实例
        /// </summary>
        static SshExec exec = null;


        public HandHandleMode()
        {
            InitializeComponent();
        }


        private object lockObject = new object();

        private void button1_Click(object sender, EventArgs e)
        {
            Ip= txt_IP.Text.Trim();
            User = txt_user.Text.Trim();
            DK= CommonClass.StringHander.Common.GetInt(txt_pwd.Text.Trim());
            PWD = txt_pwd.Text.Trim();

            if (txt_IP.Text.Trim() == string.Empty)
            {
                MessageBox.Show("IP必须填写！");
                return ;
            }
            if (txt_user.Text.Trim() == string.Empty)
            {
                MessageBox.Show("用户名必须填写！");
                return ;
            }
            if (txt_pwd.Text.Trim() == string.Empty)
            {
                MessageBox.Show("密码必须填写！");
                return;
            }
            exec = new SshExec(Ip,User,PWD);
            try
            {
                Delegate_DoIt doit = new Delegate_DoIt(exec.Connect);

                form2 = new Form2();
                form2.Owner = this;
             
                form2.ShowDialog();
                //doit.BeginInvoke(new AsyncCallback(this.GetResultCallBack), IsConnected);

            }
            catch { 

            }

        }
    
        public void SetBindControl(bool isConnected)
        {
            if (isConnected)
            {
                this.label3.Text = "已连接";
                this.label3.ForeColor = System.Drawing.Color.Green;
                this.form2.Close();
            }
            else
            {
                this.label3.Text = "连接失败";
                this.label3.ForeColor = System.Drawing.Color.Red;
                this.form2.Close();
            }
        }
        private void txt_IP_KeyPress(object sender, KeyPressEventArgs e)
        {

            TextBox textbox = (TextBox)sender;
            e.Handled = true;  
            if ((e.KeyChar >= 47 && e.KeyChar <= 58) || e.KeyChar == 8)
            {
                e.Handled = false;
            }
            if (e.KeyChar == 46)                       //小数点        
            {
                if (textbox.Text.Length <= 0)
                {
                    e.Handled = true;           //小数点不能在第一位   
                }
                else
                {
                    e.Handled = false;
                    //float f;
                    //if (float.TryParse(textbox.Text + e.KeyChar.ToString(), out f))
                    //{
                    //    e.Handled = false;
                    //}
                }
            }  
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string SessionLog = string.Empty;
            string Result = string.Empty;
            if (exec != null && exec.Connected)
            {
                try
                {
                    string Cmd = txt_cmd.Text.Trim();
                    SessionLog=exec.RunCommand(Cmd);
                   
                    this.txt_Result.Text = SessionLog;//
                    this.txt_Result.SelectionStart = this.txt_Result.Text.Length;
                    this.txt_Result.SelectionLength = 0;
                    this.txt_Result.ScrollToCaret();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString());

                    //添加成Log 
                }
            }
            else
            {
                MessageBox.Show("连接超时,请重新连接");
            }
        }


        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("保存处理....");
        }

        private void HandHandleMode_Load(object sender, EventArgs e)
        {
            if (Ip != string.Empty)
            {
                txt_IP.Text = Ip;
                txt_user.Text = User;
                txt_pwd.Text = PWD;
            }


        }

        private void txt_IP_TextChanged(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void txt_user_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (!checkBox1.Checked) txt_pwd.PasswordChar = '*'; else txt_pwd.PasswordChar = (char)0;
        }
    }
}
